

export default {
    name: "flowDefList",
    data() {
        return {
            searchForm: {
                name: '',
                code: ''
            },
            tableData: [],
            pager: {
                page: 0,
                size: 20,
                total: 0
            },
            dialogFormVisible: false,
            designerVisible: false,
            viewHide: false,
            editCode: false,
            workflowTemplate: {
                name: '',
                code: '',
                isUsed: '1',
                remarks: '',
                createUser: '',
                createDate:  new Date(),
                version: '1'
            },
            dialogTitle: '新增模板',
            entityId: '',
            designerUrl: '',
            flowState: [{key:"1",label:"启用"},{key:"0",label:"禁用"}],
            useable: true,
          fullscreen: true
        }
    },
    computed: {
        height() {
            return `calc(100vh - 350px)`;
        }
    },
    mounted() {
        this.getList();
    },
    methods: {
        // 关闭弹窗
        handleFormClose: function () {
            this.workflowTemplate = {
                id:'',
                name: '',
                code: '',
                isUsed: '1',
                remarks: '',
                createUser: '',
                createDate: new Date(),
                version: '1',
            };
            this.viewHide = false;
            this.editCode = false;
            this.dialogTitle = '新增模板';
            this.dialogFormVisible = false;
            this.entityId = '';
        },
        handleDesignerClose: function() {
            this.designerVisible = false;
        }
        ,
        // 查看
        view: function (item) {
            this.dialogTitle = '查看模板';
            this.dialogFormVisible = true;
            this.viewHide = true;
            this.editCode = true;
            this.workflowTemplate = {
                name: item.name || '',
                code: item.code || '',
                isUsed: item.isUsed || '',
                remarks: item.remarks || '',
                createUser: item.createUser || '',
                createDate: item.createDate || new Date(),
                version: item.version || '',
            }
        },
        // 修改
        edit: function (item) {

            this.dialogTitle = '修改模板';
            this.dialogFormVisible = true;
            this.editCode = false;
            this.entityId = item.id;
            this.workflowTemplate = {
                name: item.name || '',
                code: item.code || '',
                isUsed: item.isUsed || '',
                remarks: item.remarks || '',
                createUser: item.createUser || '',
                createDate: item.createDate || new Date(),
                version: item.version || '',
            };

        },
        // 删除
        deleteById: function (id) {
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let list={};
                list.id=id;
                this.$api.post(  '/wf/workflowTemplate/delete/batch',{
                    id:list
                }).then(res => {
                    if (res.status === '0') {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList();
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.errorMessage[0]
                        });
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 保存
        doSave: function () {
            let user=JSON.parse(sessionStorage.getItem("user"));
            let params  = {
                name: this.workflowTemplate.name,
                code: '',
                isUsed: this.workflowTemplate.isUsed,
                remarks: this.workflowTemplate.remarks,
                createUser: user.id,
                createDate: new Date(),
                version: this.workflowTemplate.version,
            };
            if (this.entityId === '') {
                params.createDate = new Date();
            } else {
                params.id = this.entityId;
            }
            this.$api.post(   '/wf/workflowTemplate/save', params).then(res => {
                if (res.status === '0') {
                    this.handleFormClose();
                    this.$message({
                        type: 'success',
                        message: '保存成功!'
                    });
                    this.getList();
                }
            });
        },
        // 获取模板列表
        getList: function () {
            let params = {
                page: this.pager.page === 0 ? this.pager.page : this.pager.page - 1,
                size: this.pager.size,
                name: this.searchForm.name,
                code: this.searchForm.code
            };
            this.$api.post(  '/wf/workflowTemplate/index', params).then(res => {
                if (res.status === '0') {
                    this.tableData = res.data.resContent;
                    this.pager.total = res.data.total;
                }
            });
        },
        design: function(item){
            let curWwwPath = window.location.href;
            let pos = curWwwPath.indexOf("#" +this.$route.path);
            let localhostPath = curWwwPath.substring(0, pos);
            this.designerUrl = localhostPath + "static/gui/template.html?id=" + item.id + "&baseURL=" + encodeURIComponent( this.baseURL);
            this.designerVisible = true;
        },
        stateChange: function (val) {
            this.workflowTemplate.isUsed = val == true ? "1" : "0";
        },
        doDesign: function(){
            this.$refs.ifrm.contentWindow.design();
            this.designerVisible = false;
        },
        deploy: function(id){
            this.$api.post( '/wf/workflowTemplate/deploy?id='+id).then(res => {
                if (res.status === '0') {
                    this.$message({
                        type: 'success',
                        message: res.message
                    });
                    this.getList();
                }else{
                    this.$message({
                        type: 'error',
                        message: res.message
                    });
                }
            });
        },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pager.size = val;
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pager.page = val;
      },
    }
}
